package com.hjm.project.accounts.virtualAccount.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.hjm.common.utils.LogUtils;
import com.hjm.common.utils.request.AutoLoadData;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.project.settleInfo.settleInfo.utils.ComeMoney;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.hjm.project.accounts.virtualAccount.mapper.VirtualAccountMapper;
import com.hjm.project.accounts.virtualAccount.domain.VirtualAccount;
import com.hjm.project.accounts.virtualAccount.service.IVirtualAccountService;
import com.hjm.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 虚户Service业务层处理
 * 
 * @author maker-platform
 * @date 2022-03-16
 */
@Service
public class VirtualAccountServiceImpl implements IVirtualAccountService {
    @Resource
    private VirtualAccountMapper virtualAccountMapper;

    /**
     * 查询虚户
     * 
     * @param id 虚户ID
     * @return 虚户
     */
    @Override
    public VirtualAccount selectVirtualAccountById(Integer id) {
        return virtualAccountMapper.selectVirtualAccountById(id);
    }

    /**
     * 查询虚户列表
     * 
     * @param virtualAccount 虚户
     * @return 虚户
     */
    @Override
    public List<VirtualAccount> selectVirtualAccountList(VirtualAccount virtualAccount) {
        return virtualAccountMapper.selectVirtualAccountList(virtualAccount);
    }

    /**
     * 查询虚户列表
     *
     * @param virtualAccount 虚户
     * @return 虚户 一个结果
     */
    @Override
    public VirtualAccount selectVirtualAccountOne(VirtualAccount virtualAccount) {
        return virtualAccountMapper.selectVirtualAccountOne(virtualAccount);
    }

    /**
     * 新增虚户
     * 
     * @param virtualAccount 虚户
     * @return 结果
     */
    @Override
    public int insertVirtualAccount(VirtualAccount virtualAccount) {
        return virtualAccountMapper.insertVirtualAccount(virtualAccount);
    }

    /**
     * 修改虚户
     * 
     * @param virtualAccount 虚户
     * @return 结果
     */
    @Override
    public AjaxResult updateVirtualAccount(VirtualAccount virtualAccount) {
        try{
            Map<String, Object> data = new HashMap<String, Object>();
            /** 提现 */
            data.put("type","2"); 					                 //0查余额  2提现
            data.put("mer_no",virtualAccount.getMerNo());            //商户号
            data.put("money",virtualAccount.getMoney()); 	         //金额  元
            data.put("drawal_type","4"); 			                 //提现类型 4平台提现
            String sendData = JSONObject.toJSONString(data);
            LogUtils.logInfo("请求IP:" + AutoLoadData.vmAccTxIp);
            LogUtils.logInfo("请求端口:" + AutoLoadData.vmAccTxPort);
            LogUtils.logInfo("请求参数:" + sendData);
            String resultBody = ComeMoney.socket_TX(AutoLoadData.vmAccTxIp, AutoLoadData.vmAccTxPort, sendData);
            if (StringUtils.isBlank(resultBody)) {
                return AjaxResult.error("提现消息返回为空");
            }

            if ("-1".equals(resultBody)) {
                return AjaxResult.error("提现失败");
            }

            LogUtils.logInfo("响应消息:" + resultBody);
            Map map = JSONObject.parseObject(resultBody, Map.class);
            String note = String.valueOf(map.get("note"));
            if ("00".equals(String.valueOf(map.get("rtCode")))) {
                return AjaxResult.success(note);
            } else {
                return AjaxResult.error(note);
            }
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.logInfo("异常消息:" + e.getMessage());
            return AjaxResult.error("提现异常" + e.getMessage());
        }
    }

    /**
     * 删除虚户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteVirtualAccountByIds(String ids) {
        return virtualAccountMapper.deleteVirtualAccountByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除虚户信息
     * 
     * @param id 虚户ID
     * @return 结果
     */
    @Override
    public int deleteVirtualAccountById(Integer id) {
        return virtualAccountMapper.deleteVirtualAccountById(id);
    }
}
